<?php

namespace app\tiktok\logic;
use think\facade\Db;

class VideoLogic extends BasicLogic
{

    /**
     * @desc    达人基础信息列表
     * @date    2025-07-24
     * @author  luckyXiong
     */
    public function getAdvList($params=[]){
        $limit            = getIsset($params,'limit',10);
        $expert_name      = getIsset($params,'expert_name');
        $adv_name         = getIsset($params,'adv_name');
        $fb_admin_id      = getIsset($params,'fb_admin_id');
        $sku              = getIsset($params,'sku');
        $publish_status   = getIsset($params,'publish_status');
        $start_time       = getIsset($params,'start_time');
        $end_time         = getIsset($params,'end_time');
        $create_time      = getIsset($params,'create_time');
        $where            = [];


        if($publish_status){
            $where[] = ['publish_status', '=', $publish_status];
        }

        if($expert_name){
            $where[] = ['expert_name', 'like', '%' . $expert_name . '%'];
        }

        if($adv_name){
            $where[] = ['adv_name', 'like', '%' . $adv_name . '%'];
        }

        if($sku){
            $where[] = ['sku', 'like', '%' . $sku . '%'];
        }

        if($fb_admin_id){
            $where[] = ['fb_admin_id', '=',$fb_admin_id];
        }

        if (!empty($start_time)) {
            $where[]  = ['start_time', '>=', date('Y-m-d H:i:s',strtotime($start_time[0]))];
            $where[]  = ['start_time', '<=', date('Y-m-d H:i:s',strtotime($start_time[1]) + 3600*24-1)];
        }

        if (!empty($end_time)) {
            $where[]  = ['end_time', '>=', date('Y-m-d H:i:s',strtotime($end_time[0]))];
            $where[]  = ['end_time', '<=', date('Y-m-d H:i:s',strtotime($end_time[1]) + 3600*24-1)];
        }

        if (!empty($create_time)) {
            $where[]  = ['created_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['created_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }

        $field = "*";
        $res   = Db::name('tik_kol_video_ads')
            ->field($field)
            ->where($where)
            ->order('create_time DESC')
            ->paginate($limit);
        $list = $res->items()?:[];

        //批量获取创建人;
        $operater_ids    = array_column($list,'fb_admin_id');
        $admin_users     = Db::name('system_admin')->field('id,username')->whereIn('id',$operater_ids)->select()->toArray();
        $admin_users_map = array_column($admin_users,null,'id');
        foreach($list as $k=>$v){
            $admin_users_info      = $admin_users_map[$v['fb_admin_id']]??'';
            $list[$k]['operater']  = $admin_users_info['username']??'';
        }


        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }


    /**
     * @desc    素材订单新增接口
     * @date    2025-07-29
     * @author  luckyXiong
     */
    public function addAdv($params=[]){
        $expert_id = $params['expert_id']??'';
        $expert_name       = Db::name('tik_kol')
            ->where(['expert_id'=>$expert_id])
            ->value('expert_name');

        $sku         = $params['sku']??'';
        $publish_url = $params['publish_url']??'';
        $insert_info = [
            'expert_id'       =>$expert_id,
            'expert_name'     =>$expert_name??'',
            'publish_url'     =>$publish_url,
            'ads_name'        =>$params['ads_name']??'',
            'adv_name'        =>$params['adv_name']??'',
            'sku'             =>$sku,
            'adv_platform_id' =>$params['adv_platform_id']??'',
            'publish_status'  =>$params['publish_status']??'1',
            'fb_admin_id'     =>$params['fb_admin_id']??'',
            'spend'           =>$params['spend']??'',
            'order_nums'      =>$params['order_nums']??'0',
            'income'          =>$params['income']??'',
            'cpm'             =>$params['cpm']??'',
            'cpc'             =>$params['cpc']??'',
            'roi'             =>$params['roi']??'',
            'ctr'             =>$params['ctr']??'',
            'start_time'      =>$params['start_time']??'',
            'end_time'        =>$params['end_time']??'',
            'update_time'     =>date("Y-m-d H:i:s", time()),
            'create_time'     =>date("Y-m-d H:i:s", time()),
        ];

        $where    = [];
        $where[]  = ['publish_url', 'like', '%' . $publish_url . '%'];
        $where[]  = ['sku', 'like', '%' . $sku. '%'];

        $kol_info =  Db::name('tik_kol_video_ads')
            ->where($where)
            ->find();
        if(!$kol_info){
            Db::name('tik_kol_video_ads')->insert($insert_info);
        }
        return  $this->jsonMessage(200,true,'操作成功!','');
    }


    /**
     * @desc    素材订单编辑接口
     * @date    2025-07-29
     * @author  luckyXiong
     */
    public function editAdv($params=[]){
        $id        = $params['id']??'';
        $expert_id = $params['expert_id']??'';
        $expert_name       = Db::name('tik_kol')
            ->where(['expert_id'=>$expert_id])
            ->value('expert_name');

        $sku         = $params['sku']??'';
        $publish_url = $params['publish_url']??'';
        $update_info = [
            'expert_id'       =>$expert_id,
            'expert_name'     =>$expert_name??'',
            'publish_url'     =>$publish_url,
            'ads_name'        =>$params['ads_name']??'',
            'adv_name'        =>$params['adv_name']??'',
            'sku'             =>$sku,
            'adv_platform_id' =>$params['adv_platform_id']??'',
            'publish_status'  =>$params['publish_status']??'1',
            'fb_admin_id'     =>$params['fb_admin_id']??'',
            'spend'           =>$params['spend']??'',
            'order_nums'      =>$params['order_nums']??'0',
            'income'          =>$params['income']??'',
            'cpm'             =>$params['cpm']??'',
            'cpc'             =>$params['cpc']??'',
            'roi'             =>$params['roi']??'',
            'ctr'             =>$params['ctr']??'',
            'start_time'      =>$params['start_time']??'',
            'end_time'        =>$params['end_time']??'',
            'update_time'     =>date("Y-m-d H:i:s", time()),
        ];
        Db::name('tik_kol_video_ads')->where(['id'=>$id])->update($update_info);
        return  $this->jsonMessage(200,true,'操作成功!','');
    }



    /**
     * @desc    素材订单删除接口
     * @date    2025-07-29
     * @author  luckyXiong
     */
    public function delAdv($params){
        $id           = $params['id']??'';
        Db::name('tik_kol_video_ads')->where(['id'=>$id])->delete();
        return  $this->jsonMessage(200,true,'操作成功!','');
    }



    /**
     * @desc    达人素材列表接口
     * @date    2025-07-29
     * @author  luckyXiong
     */
    public function getVideoList($params){
        $limit            = getIsset($params,'limit',10);
        $expert_name      = getIsset($params,'expert_name');
        $creation_code    = getIsset($params,'creation_code');
        $fb_admin_id      = getIsset($params,'fb_admin_id');
        $sku              = getIsset($params,'sku');
        $publish_time     = getIsset($params,'publish_time');
        $create_time      = getIsset($params,'create_time');
        $where            = [];

        if($expert_name){
            $where[] = ['expert_name', 'like', '%' . $expert_name . '%'];
        }

        if($creation_code){
            $where[] = ['creation_code', 'like', '%' . $creation_code . '%'];
        }

        if($sku){
            $where[] = ['sku', 'like', '%' . $sku . '%'];
        }

        if($fb_admin_id){
            $where[] = ['fb_admin_id', '=',$fb_admin_id];
        }

        if (!empty($publish_time)) {
            $where[]  = ['publish_time', '>=', date('Y-m-d H:i:s',strtotime($publish_time[0]))];
            $where[]  = ['publish_time', '<=', date('Y-m-d H:i:s',strtotime($publish_time[1]) + 3600*24-1)];
        }

        if (!empty($create_time)) {
            $where[]  = ['created_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['created_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }

        $field = "*";
        $res   = Db::name('tik_kol_video')
            ->field($field)
            ->where($where)
            ->order('create_time DESC')
            ->paginate($limit);
        $list = $res->items()?:[];

        //批量获取创建人;
        $operater_ids    = array_column($list,'fb_admin_id');
        $admin_users     = Db::name('system_admin')->field('id,username')->whereIn('id',$operater_ids)->select()->toArray();
        $admin_users_map = array_column($admin_users,null,'id');
        foreach($list as $k=>$v){
            $admin_users_info      = $admin_users_map[$v['fb_admin_id']]??'';
            $list[$k]['operater']  = $admin_users_info['username']??'';
        }

        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }


    /**
     * @desc    达人素材新增接口
     * @date    2025-07-29
     * @author  luckyXiong
     */
    public function addVideo($params=[]){
       $shop_id     = $params['shop_id']??'';
       $expert_id   = $params['expert_id']??'';
       $expert_name = Db::name('tik_kol')
        ->where(['expert_id'=>$expert_id])
        ->value('expert_name');

        $sku         = $params['sku']??'';
        $video_url   = $params['video_url']??'';
        $insert_info = [
            'shop_id'         =>$shop_id,
            'expert_id'       =>$expert_id,
            'expert_name'     =>$expert_name??'',
            'creation_code'   =>$params['creation_code']??'',
            'video_url'       =>$video_url,
            'sku'             =>$sku,
            'remarks'         =>$params['remarks']??'',
            'remarks_exception'=>$params['remarks_exception']??'',
            'fb_admin_id'     =>$params['fb_admin_id']??'',
            'fb_admin_names'  =>$params['fb_admin_names']??'',
            'spend'           =>$params['spend']??'',
            'income'          =>$params['income']??'',
            'order_nums'      =>$params['order_nums']??'0',
            'roi'             =>$params['roi']??'',
            'publish_time'    =>$params['publish_time']??'',
            'update_time'     =>date("Y-m-d H:i:s", time()),
            'create_time'     =>date("Y-m-d H:i:s", time()),
        ];

        $where    = [];
        $where[]  = ['video_url', 'like', '%' . $video_url . '%'];
        $where[]  = ['shop_id', '=', $shop_id];

        $kol_info =  Db::name('tik_kol_video')
            ->where($where)
            ->find();
        if(!$kol_info){
            Db::name('tik_kol_video')->insert($insert_info);
        }
        return  $this->jsonMessage(200,true,'操作成功!','');
    }


    /**
     * @desc    达人素材编辑接口
     * @date    2025-07-29
     * @author  luckyXiong
     */
    public function editVideo($params=[]){
        $id          = $params['id']??'';
        $shop_id     = $params['shop_id']??'';
        $expert_id   = $params['expert_id']??'';
        $expert_name = Db::name('tik_kol')
            ->where(['expert_id'=>$expert_id])
            ->value('expert_name');

        $sku         = $params['sku']??'';
        $video_url   = $params['video_url']??'';
        $update_info = [
            'shop_id'         =>$shop_id,
            'expert_id'       =>$expert_id,
            'expert_name'     =>$expert_name??'',
            'creation_code'   =>$params['creation_code']??'',
            'video_url'       =>$video_url,
            'sku'             =>$sku,
            'remarks'         =>$params['remarks']??'',
            'remarks_exception'=>$params['remarks_exception']??'',
            'fb_admin_id'     =>$params['fb_admin_id']??'',
            'fb_admin_names'  =>$params['fb_admin_names']??'',
            'spend'           =>$params['spend']??'',
            'income'          =>$params['income']??'',
            'order_nums'      =>$params['order_nums']??'0',
            'roi'             =>$params['roi']??'',
            'publish_time'    =>$params['publish_time']??'',
            'update_time'     =>date("Y-m-d H:i:s", time()),
            'create_time'     =>date("Y-m-d H:i:s", time()),
        ];
        Db::name('tik_kol_video')->where(['id'=>$id])->update($update_info);
        return  $this->jsonMessage(200,true,'操作成功!','');
    }


    /**
     * @desc    达人素材删除接口
     * @date    2025-07-29
     * @author  luckyXiong
     */
    public function delVideo($params=[]){
        $id = $params['id']??'';
        Db::name('tik_kol_video')->where(['id'=>$id])->delete();
        return  $this->jsonMessage(200,true,'操作成功!','');
    }















}